package com.rfid.a252robot.activity;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;

import com.rfid.a252robot.App;
import com.rfid.a252robot.net.BookLibApi;
import com.rfid.a252robot.net.BoxLibApi;
import com.rfid.common.log.LogUtils;
import com.rfid.common.util.SharedPreferencesUtil;
import com.rfid.manager.DoorBoxManager;
import com.rfid.manager.ReadTagManager;
import com.rfid.util.Util;

public class SplashActivity extends Activity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SharedPreferencesUtil.init(getApplicationContext(),"robot", Context.MODE_PRIVATE);
        if (!checkPermission()) {
            ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},100);
        }

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        boolean r = checkPermission();
        if (!r) {
            Toast.makeText(this,"无法获取权限",Toast.LENGTH_SHORT).show();
            finish();
        }
    }

    private boolean checkPermission() {
        if (ActivityCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
            System.out.println("checkPermission GRANTED");
            App.getApp().init();
            ReadTagManager.INSTANCE.init(getApplicationContext(), false, new ReadTagManager.IonDetectDeviceListener() {
                @Override
                public void detectComplete() {
                    LogUtils.v("detectComplete");
                }
            });
            BoxLibApi.INSTANCE.init();
            BookLibApi.INSTANCE.init();
            DoorBoxManager.INSTANCE.init(getApplicationContext());
            Util.startActivity(this, MainActivity.class);
            finish();
            return true;
        }
        return false;
    }
}
